﻿/*
    Copyright (c) 2007 - 2009, Carlos Guzmán Álvarez

    All rights reserved.

    Redistribution and use in source and binary forms, with or without modification, 
    are permitted provided that the following conditions are met:

        * Redistributions of source code must retain the above copyright notice, 
          this list of conditions and the following disclaimer.
        * Redistributions in binary form must reproduce the above copyright notice, 
          this list of conditions and the following disclaimer in the documentation and/or 
          other materials provided with the distribution.
        * Neither the name of the author nor the names of its contributors may be used to endorse or 
          promote products derived from this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using System;
using System.Collections.Generic;
using Framework.Net.Xmpp.Core;
using Framework.Net.Xmpp.Serialization.InstantMessaging.Client;

namespace Framework.Net.Xmpp.InstantMessaging
{
    /// <summary>
    /// Represents a chat conversation with a contact.
    /// </summary>
	public sealed class XmppChat
	{
		#region · Events ·

		/// <summary>
		/// Occurs when a new message is received
		/// </summary>
        public event EventHandler ReceivedMessage;

		/// <summary>
		/// Occurs before the chat is closed
		/// </summary>
        public event EventHandler ChatClosing;

		/// <summary>
		/// Occurs when the chat is closed
		/// </summary>
        public event EventHandler ChatClosed;

		#endregion

		#region · Fields ·

		private XmppContact         contact;
		private XmppSession         session;
        private Queue<XmppMessage>  pendingMessages;
		
		#endregion

		#region · Properties ·

        /// <summary>
        /// Gets the contact.
        /// </summary>
        /// <value>The contact.</value>
		public XmppContact Contact
		{
			get { return this.contact; }
		}

        /// <summary>
        /// Gets the session.
        /// </summary>
        /// <value>The session.</value>
        public XmppSession Session
		{
			get { return this.session; }
		}

        /// <summary>
        /// Gets the pending messages
        /// </summary>
        public Queue<XmppMessage> PendingMessages
        {
            get { return this.pendingMessages; }
        }

		#endregion

		#region · Constructors ·

        /// <summary>
        /// Initializes a new instance of the <see cref="T:XmppChat"/> class.
        /// </summary>
        /// <param name="session">The session.</param>
        /// <param name="contact">The contact.</param>
        internal XmppChat(XmppSession session, XmppContact contact)
        {
			this.session            = session;
            this.contact            = contact;
            this.pendingMessages    = new Queue<XmppMessage>();

            this.WireEvents();
        }

		#endregion

		#region · Methods ·

        /// <summary>
        /// Sends the message.
        /// </summary>
        /// <param name="message">The message.</param>
        public string SendMessage(string message)
        {
            if (this.session == null)
            {
                throw new InvalidOperationException("Chat session is closed.");
            }

            MessageBody body = new MessageBody();
            body.Value = message;

            Message chatMessage = new Message();

            chatMessage.ID      = XmppIdentifierGenerator.Generate();
            chatMessage.Type    = MessageType.Chat;
            chatMessage.From    = this.session.UserId.ToString();
            chatMessage.To      = this.Contact.ContactId.ToString();

            chatMessage.Items.Add(body);

            this.session.Send(chatMessage);

            return chatMessage.ID;
        }

        /// <summary>
        /// Closes this instance.
        /// </summary>
        public void Close()
        {
			if (this.ChatClosing != null)
			{
				this.ChatClosing(this, new EventArgs());
			}

            this.pendingMessages.Clear();
            this.UnwireEvents();

            this.pendingMessages    = null;
           
			if (this.ChatClosed != null)
			{
				this.ChatClosed(this, new EventArgs());
			}

            this.session = null;
            this.contact = null;
        }

		#endregion

        #region · Chat Messages Processing Methods ·

		internal void ChatMessageReceived(XmppMessageReceivedEventArgs e)
		{
			if (e.Message.From.BareIdentifier == this.Contact.ContactId.BareIdentifier)
			{
                this.pendingMessages.Enqueue(e.Message);

				if (this.ReceivedMessage != null)
				{
                    this.ReceivedMessage(e, new EventArgs());
				}
			}
		}

		#endregion

        #region · Event Wiring ·

        private void WireEvents()
        {
            this.session.StateChanged += new EventHandler<XmppSessionStateChangedEventArgs>(OnSessionStateChanged);
        }

        private void UnwireEvents()
        {
            this.session.StateChanged -= new EventHandler<XmppSessionStateChangedEventArgs>(OnSessionStateChanged);
        }

        #endregion

        #region · Session Event Handlers ·

        private void OnSessionStateChanged(object sender, XmppSessionStateChangedEventArgs e)
		{
            if (this.session.State == XmppSessionState.LoggingOut)
            {
                this.Close();
            }
        }

		#endregion
	}
}
